package org.thinkinbunny.financial;

import org.thinkinbunny.financial.activity.MainActivity;
import org.thinkinbunny.financial.activity.MyActivity;
import org.thinkinbunny.financial.data.UpdateVerHelper;
import org.thinkinbunny.financial.encrypt.Encrypt;
import org.thinkinbunny.financial.util.Constant;
import org.thinkinbunny.financial.util.Log;
import org.thinkinbunny.financial.util.MiscToolBox;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class Lanucher extends MyActivity {
	private final long m_dwSplashTime = 1000;
	private boolean m_bPaused = false;
	private boolean m_bSplashActive = true;
	private Dialog dialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lanucher);
Encrypt encrypt=new Encrypt();
Log.i("JNI_NDK", "JAVA>>"+encrypt.des(""));
Log.i("JNI_NDK", "JAVA>>"+encrypt.desRes(""));
		// Log.i("Lanucher", syncHelper.getJsonArrayFromDB().toString());
		SharedPreferences sharedPreferences = getSharedPreferences(
				Constant.DeviceInfo, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("deviceInfo",
				UpdateVerHelper.getVerMisc(Lanucher.this));
		editor.putString("version",
				UpdateVerHelper.getVersion(Lanucher.this));
		// editor.putString("uuid", UpdateVerHelper.getVerMisc(Lanucher.this));
		editor.commit();
		if (MiscToolBox.isShowNotice())
			showNotice();
		else
		{
			

			init();
			}
		

	}
	void init(){
		Thread splashTimer = new Thread() {
			@Override
			public void run() {
				try {
					// wait loop
					long ms = 0;
					while (m_bSplashActive && ms < m_dwSplashTime) {
						sleep(100);

						if (!m_bPaused)
							ms += 100;
					}

					startActivity(new Intent(Lanucher.this, MainActivity.class));
				} catch (Exception ex) {
					Log.e("Splash", ex.getMessage());
				} finally {
					finish();
				}
			}
		};
		splashTimer.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		m_bPaused = true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		m_bPaused = false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU:
			m_bSplashActive = false;
			break;
		case KeyEvent.KEYCODE_BACK:
			/* 两种退出方法 */
			/* System.exit(0); */
			/* android.os.Process.killProcess(android.os.Process.myPid()); */
			android.os.Process.killProcess(android.os.Process.myPid());
			break;
		default:
			break;
		}
		return true;
	}

	private void showNotice (){
		LayoutInflater factory = LayoutInflater.from(this);
		final View buttonView = factory.inflate(R.layout.dialog_pronouncement,
				null);
		dialog = new Dialog(this){
			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
//					showExitDialog();
					return true;
				}
				return super.onKeyDown(keyCode, event);
			}
		};
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setCancelable(false);
//		dialog.setTitle("");
		dialog.setContentView(buttonView);
		Button agree_button = (Button) buttonView.findViewById(R.id.agree_button);
		agree_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				MiscToolBox.saveShowNotice(false);
				dialog.dismiss();
				init();
			}
		});
		Button refuse_button = (Button) buttonView.findViewById(R.id.refuse_button);
		refuse_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(dialog!=null){
					dialog.dismiss();
					  finish();
				}
			}
		});
//		View launchView = (View)Launch.this.findViewById(R.id.Splash);
//		launchView.setBackgroundColor(0xFF000000);
		dialog.show();
	}
}
